package com.suli.multimedia.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suli.multimedia.entity.Article;
import com.suli.multimedia.entity.ArticleContent;
import com.suli.multimedia.entity.User;
import com.suli.multimedia.entity.vo.ArticleVO;
import com.suli.multimedia.enums.ArticleType;
import com.suli.multimedia.mapper.ArticleMapper;
import com.suli.multimedia.mapper.UserMapper;
import com.suli.multimedia.service.ArticleContentService;
import com.suli.multimedia.service.ArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author SuLi
 * @since 2022-03-17
 */
@Service
@Transactional
@SuppressWarnings("all")
@Slf4j
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    @Autowired
    ArticleContentService contentService;
    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public boolean createOneArticle(ArticleVO articleVO) {
        Article article=new Article(articleVO.getUserId(),articleVO.getArticleTitle(),articleVO.getArticleType());
        if(this.save(article)){
            ArticleContent articleContent=new ArticleContent(article.getArticleId(), articleVO.getArticleContent());
            if(contentService.save(articleContent)){
                return true;
            }
        }
        return false;
    }

    @Override
    public Map<String, Object> getArticleByPage(Long page, Long limit,Integer artilceType) {
        Page<Article> page1=new Page<>(page,limit);
        QueryWrapper<Article> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("article_type", artilceType).orderByDesc("publish_time");;
        Page<Article> articlePage = articleMapper.selectPage(page1, queryWrapper);
        long total = articlePage.getTotal();
        List<Article> records = articlePage.getRecords();
        Map<String,Object> map=new HashMap<>();
        map.put("total", total);
        map.put("news", records);
        return map;
    }

    @Override
    public Map<String, Object> getArticleByUserId(String userId, Long page, Long limit, String articleType) {
        Page<Article> page1=new Page<>(page,limit);
        QueryWrapper<Article> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .eq("article_type", ArticleType.valueOf(articleType));
        Page<Article> articlePage = articleMapper.selectPage(page1, queryWrapper);
        HashMap<String,Object> map=new HashMap<>();
        map.put("total", articlePage.getTotal());
        map.put("articleList", articlePage.getRecords());
        return map;
    }

    @Cacheable("article")
    @Override
    public Map<String,Object> getArticleById(Long articleId) {
        Article article = this.getById(articleId);
        if(!ObjectUtils.isEmpty(article)){
            ArticleContent articleContent = contentService.getById(articleId);
            User user = userMapper.selectById(article.getUserId());
            if(!ObjectUtils.isEmpty(articleContent)){
                ArticleVO articleVO = new ArticleVO(
                        article.getArticleId(),
                        article.getArticleType(),
                        article.getUserId(),
                        article.getArticleTitle(),
                        article.getPublishTime(),
                        article.getIsDeleted(),
                        article.getViews(),
                        articleContent.getArticleContent(),
                        user.getUserName());
                Map<String,Object> map=new HashMap<>();
                map.put("article", articleVO);
                return map;
            }
        }
        return null;
    }

    @Override
    public Map<String, Object> getArticleByTitle(String title, Integer articleType) {
        QueryWrapper<Article> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("article_type", articleType).like("article_title", title);
        List<Article> articles = articleMapper.selectList(queryWrapper);
        HashMap<String,Object> map=new HashMap<>();
        if(!ObjectUtils.isEmpty(articles)){
            map.put("articleList", articles);
            return map;
        }
        map.put("articleList", "");
        return map;
    }

    @Override
    public Map<String, Object> getArticleListDeleted(Integer page, Integer limit, Integer articleType) {
        page=(page-1)*10;
        List<Article> articleIsDeleted = articleMapper.getArticleIsDeleted(articleType, page, limit);
        Integer articleCount = articleMapper.getArticleCount(articleType);
        Map<String, Object> map=new HashMap<>();
        map.put("news", articleIsDeleted);
        map.put("total", articleCount);
        return map;
    }

    @Override
    public void deleteArticleDeleted(List<Integer> articleIds) {
        articleMapper.DeleteBatchByIds(articleIds);
        articleMapper.DeleteContentBatchByIds(articleIds);
    }

    @Override
    public Map<String, Object> getDetailDeleted(Integer articleId) {
        ArticleVO articleDetail = articleMapper.getArticleDetail(articleId);
        Map<String,Object> map = new HashMap<>();
        map.put("article", articleDetail);
        return map;
    }

    @Override
    public boolean recovery(List<Integer> ids) {
        int i = articleMapper.recoveryArticles(ids);
        int i1 = articleMapper.recoveryContent(ids);
        return i>0 && i1>0;
    }
}
